package com.lewis.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lewis.core.base.domain.entity.SysDictData;
import com.lewis.core.utils.DictUtil;
import com.lewis.core.utils.StringUtil;
import com.lewis.system.service.ISysDictDataService;
import com.lewis.system.mapper.SysDictDataMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 字典 业务层处理
 *
 * @author Lewis
 */
@Service
public class SysDictDataServiceImpl extends ServiceImpl<SysDictDataMapper, SysDictData> implements ISysDictDataService {

    @Resource
    private SysDictDataMapper sysDictDataMapper;

    @Override
    public List<SysDictData> selectDictDataList(SysDictData dictData) {
        return sysDictDataMapper.selectList(
                new LambdaQueryWrapper<SysDictData>()
                        .eq(StringUtil.isNotEmpty(dictData.getDictType()), SysDictData::getDictType, dictData.getDictType())
                        .eq(StringUtil.isNotEmpty(dictData.getStatus()), SysDictData::getStatus, dictData.getStatus())
                        .like(StringUtil.isNotEmpty(dictData.getDictLabel()), SysDictData::getDictLabel, dictData.getDictLabel())
                        .orderByAsc(SysDictData::getStatus)
        );
    }

    @Override
    public SysDictData getOneByDictTypeAndDictValue(String dictType, String dictValue) {
        return sysDictDataMapper.selectOne(
                new LambdaQueryWrapper<SysDictData>()
                        .eq(SysDictData::getDictType, dictType)
                        .eq(SysDictData::getDictValue, dictValue)
        );
    }

    @Override
    public void deleteDictDataByIds(Long[] ids) {
        for (Long id : ids) {
            SysDictData data = sysDictDataMapper.selectById(id);
            sysDictDataMapper.deleteById(id);
            List<SysDictData> dictDataList = this.selectDictDataByType(data.getDictType());
            DictUtil.setDictCache(data.getDictType(), dictDataList);
        }
    }

    @Override
    public int insert(SysDictData data) {
        int row = sysDictDataMapper.insert(data);
        if (row > 0) {
            List<SysDictData> dictDatas = this.selectDictDataByType(data.getDictType());
            DictUtil.setDictCache(data.getDictType(), dictDatas);
        }
        return row;
    }

    @Override
    public int updateDictData(SysDictData data) {
        int row = sysDictDataMapper.updateById(data);
        if (row > 0) {
            List<SysDictData> dictDatas = this.selectDictDataByType(data.getDictType());
            DictUtil.setDictCache(data.getDictType(), dictDatas);
        }
        return row;
    }

    @Override
    public List<SysDictData> selectDictDataByType(String dictType) {
        return sysDictDataMapper.selectList(
                new LambdaQueryWrapper<SysDictData>()
                        .eq(SysDictData::getStatus, "0")
                        .eq(SysDictData::getDictType, dictType)
                        .orderByAsc(SysDictData::getDictSort)
        );
    }
}
